﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class user : ModulePage
    {

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.CloseConn();
            Response.Redirect("search.aspx?user_id=" + this.ViewState["UserId"].ToString() + "&date1=" + this.Date1.Value + "&date2=" + this.Date2.Value);
        }
        protected void btnSomeDay_Click(object sender, EventArgs e)
        {
            base.CloseConn();
            Response.Redirect("someday.aspx?user_id=" + this.ViewState["UserId"].ToString() + "&somedate=" + this.SomeDate.Value);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num5;
                DateTime time2;
                DateTime time3;
                DateTime time4;
                string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : base.User.Identity.Name;
                this.ViewState["UserId"] = userId;
                int num = 30;
                int num2 = 10;
                int num3 = 10;
                int num4 = 60;
                AttendParaDao dao = new AttendParaDao();
                dao.GetPara();
                num = dao.DutyIntervalBefore1;
                num2 = dao.DutyIntervalAfter1;
                num3 = dao.DutyIntervalBefore2;
                num4 = dao.DutyIntervalAfter2;
                DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, userId);
                this.litDutyName.Text = userConfig.Rows[0]["DutyName"].ToString();
                DataTable table2 = new DataTable();
                table2.Columns.Add("No", Type.GetType("System.Int32"));
                table2.Columns.Add("DutyType", Type.GetType("System.Int32"));
                table2.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
                table2.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
                table2.Columns.Add("EndTime", Type.GetType("System.DateTime"));
                table2.Columns.Add("DutyId", Type.GetType("System.Int32"));
                table2.Columns.Add("RegisterTime", Type.GetType("System.DateTime"));
                table2.Columns.Add("RegisterFlag", Type.GetType("System.Int32"));
                table2.Columns.Add("RegisterIp", Type.GetType("System.String"));
                table2.Columns.Add("Remark", Type.GetType("System.String"));
                if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
                {
                    num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                    time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                    if (num5 == 1)
                    {
                        time3 = time2.AddMinutes((double)-num);
                        time4 = time2.AddMinutes((double)num2);
                    }
                    else
                    {
                        time3 = time2.AddMinutes((double)-num3);
                        time4 = time2.AddMinutes((double)num4);
                    }
                    table2.Rows.Add(new object[] { 1, num5, time2, time3, time4 });
                }
                if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
                {
                    num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                    time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                    if (num5 == 1)
                    {
                        time3 = time2.AddMinutes((double)-num);
                        time4 = time2.AddMinutes((double)num2);
                    }
                    else
                    {
                        time3 = time2.AddMinutes((double)-num3);
                        time4 = time2.AddMinutes((double)num4);
                    }
                    table2.Rows.Add(new object[] { 2, num5, time2, time3, time4 });
                }
                if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
                {
                    num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                    time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                    if (num5 == 1)
                    {
                        time3 = time2.AddMinutes((double)-num);
                        time4 = time2.AddMinutes((double)num2);
                    }
                    else
                    {
                        time3 = time2.AddMinutes((double)-num3);
                        time4 = time2.AddMinutes((double)num4);
                    }
                    table2.Rows.Add(new object[] { 3, num5, time2, time3, time4 });
                }
                if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
                {
                    num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                    time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                    if (num5 == 1)
                    {
                        time3 = time2.AddMinutes((double)-num);
                        time4 = time2.AddMinutes((double)num2);
                    }
                    else
                    {
                        time3 = time2.AddMinutes((double)-num3);
                        time4 = time2.AddMinutes((double)num4);
                    }
                    table2.Rows.Add(new object[] { 4, num5, time2, time3, time4 });
                }
                if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
                {
                    num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                    time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                    if (num5 == 1)
                    {
                        time3 = time2.AddMinutes((double)-num);
                        time4 = time2.AddMinutes((double)num2);
                    }
                    else
                    {
                        time3 = time2.AddMinutes((double)-num3);
                        time4 = time2.AddMinutes((double)num4);
                    }
                    table2.Rows.Add(new object[] { 5, num5, time2, time3, time4 });
                }
                if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
                {
                    num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                    time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                    if (num5 == 1)
                    {
                        time3 = time2.AddMinutes((double)-num);
                        time4 = time2.AddMinutes((double)num2);
                    }
                    else
                    {
                        time3 = time2.AddMinutes((double)-num3);
                        time4 = time2.AddMinutes((double)num4);
                    }
                    table2.Rows.Add(new object[] { 6, num5, time2, time3, time4 });
                }
                bool flag = false;
                string commandText = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + DateTime.Now.ToString("yyyy-MM-dd") + "' AND '" + DateTime.Now.ToString("yyyy-MM-dd") + "' <= [EndDate]";
                if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, commandText) != null)
                {
                    flag = true;
                }
                commandText = "SELECT * FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + userId + "' AND [RegisterTime] >='" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59'";
                DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    DataRow[] rowArray = table3.Select("RegisterType = " + table2.Rows[i]["DutyType"].ToString() + " AND RegisterTime >='" + table2.Rows[i]["BeginTime"].ToString() + "' AND RegisterTime <='" + table2.Rows[i]["EndTime"].ToString() + "'");
                    if (rowArray.Length > 0)
                    {
                        table2.Rows[i]["DutyId"] = Convert.ToInt32(rowArray[0]["DutyId"].ToString());
                        table2.Rows[i]["RegisterTime"] = Convert.ToDateTime(rowArray[0]["RegisterTime"].ToString());
                        table2.Rows[i]["RegisterFlag"] = Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString());
                        table2.Rows[i]["RegisterIp"] = rowArray[0]["RegisterIp"].ToString();
                        if (rowArray[0]["Remark"] != DBNull.Value)
                        {
                            table2.Rows[i]["Remark"] = rowArray[0]["Remark"].ToString();
                        }
                    }
                    else
                    {
                        table2.Rows[i]["RegisterFlag"] = 0;
                        if (flag)
                        {
                            table2.Rows[i]["DutyId"] = -4;
                        }
                        else
                        {
                            commandText = "SELECT [OutId] FROM [dbo].[Attend_Out] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [OutTime1] <='" + table2.Rows[i]["DutyTime"].ToString() + "' AND [OutTime2]>='" + table2.Rows[i]["DutyTime"].ToString() + "'";
                            DataTable table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                            if (table4.Rows.Count > 0)
                            {
                                table2.Rows[i]["DutyId"] = -1;
                            }
                            else
                            {
                                commandText = "SELECT [LeaveId] FROM [dbo].[Attend_Leave] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [LeaveDate1] <='" + table2.Rows[i]["DutyTime"].ToString() + "' AND [LeaveDate2]>='" + table2.Rows[i]["DutyTime"].ToString() + "'";
                                DataTable table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                if (table5.Rows.Count > 0)
                                {
                                    table2.Rows[i]["DutyId"] = -2;
                                }
                                else
                                {
                                    commandText = "SELECT [EvectionId] FROM [dbo].[Attend_Evection] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [EvectionDate1] <='" + Convert.ToDateTime(table2.Rows[i]["DutyTime"].ToString()).ToString("yyyy-MM-dd") + "' AND [EvectionDate1]>='" + Convert.ToDateTime(table2.Rows[i]["DutyTime"].ToString()).ToString("yyyy-MM-dd") + "'";
                                    DataTable table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                    if (table6.Rows.Count > 0)
                                    {
                                        table2.Rows[i]["DutyId"] = -3;
                                    }
                                }
                            }
                        }
                    }
                }
                this.rptMy.DataSource = table2;
                this.rptMy.DataBind();
                this.Date1.Value = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-1";
                this.Date2.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.SomeDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.btnSearch.Attributes.Add("onclick", "return CheckForm1()");
                this.btnSomeDay.Attributes.Add("onclick", "return CheckForm2()");
            }
        }
 

    }
}
